<template>
  <div class="detail-container">
    <div class="header-nav">
      <div class="logo">“沈誉通”信用生活综合系统</div>
      <div class="nav">
        <!-- <router-link to="/home">首页</router-link> -->
        <router-link to="/credit-life">信用生活</router-link>
        <router-link to="/credit-business">信用商业</router-link>
        <router-link to="/behavior">亲社会行为</router-link>
        <router-link to="/news">新闻资讯</router-link>
        <router-link to="/credit-management">信用管理</router-link>
      </div>
    </div>
    <div class="back-bar">
      <el-button icon="el-icon-arrow-left" @click="$router.push('/credit-life')" type="text">返回信用生活</el-button>
    </div>
    <div class="detail-main">
      <h2>图书借阅</h2>
      <div class="search-section">
        <el-input
          v-model="searchQuery"
          placeholder="搜索图书名称、作者或分类"
          prefix-icon="el-icon-search"
          class="search-input"
        >
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </div>
      <div class="books-grid">
        <div v-for="book in filteredBooks" :key="book.id" class="book-card">
          <div class="book-cover">
            <img :src="book.cover" :alt="book.title">
          </div>
          <div class="book-info">
            <h3>{{ book.title }}</h3>
            <p class="author">{{ book.author }}</p>
            <p class="category">{{ book.category }}</p>
            <div class="book-status">
              <span :class="{'status-available': book.available, 'status-borrowed': !book.available}">
                {{ book.available ? '可借阅' : '已借出' }}
              </span>
            </div>
            <el-button
              type="primary"
              :disabled="!book.available"
              class="borrow-btn"
              @click="borrowBook(book)"
            >
              借阅
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "LibraryDetail",
  data() {
    return {
      searchQuery: '',
      books: [
        {
          id: 1,
          title: '人工智能：现代方法',
          author: 'Stuart Russell / Peter Norvig',
          category: '计算机科学',
          cover: require('@/imgs/types/amzl.png'),
          available: true
        },
        {
          id: 2,
          title: '深入理解计算机系统',
          author: 'Randal E. Bryant',
          category: '计算机科学',
          cover: require('@/imgs/types/hpg.png'),
          available: false
        },
        {
          id: 3,
          title: '算法导论',
          author: 'Thomas H. Cormen',
          category: '计算机科学',
          cover: require('@/imgs/types/jsyd.png'),
          available: true
        },
        {
          id: 4,
          title: '数据库系统概念',
          author: 'Abraham Silberschatz',
          category: '计算机科学',
          cover: require('@/imgs/types/ms.png'),
          available: true
        }
      ]
    };
  },
  computed: {
    filteredBooks() {
      if (!this.searchQuery) return this.books;
      const q = this.searchQuery.toLowerCase();
      return this.books.filter(book =>
        book.title.toLowerCase().includes(q) ||
        book.author.toLowerCase().includes(q) ||
        book.category.toLowerCase().includes(q)
      );
    }
  },
  created() {
    this.recordClick();

  },
  methods: {
    async recordClick() {
      try {
        const { data: userData } = await axios.get('/users/user/basic', {
          headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
        });
        if (userData.success && userData.data) {
          this.user = userData.data;
        } else {
          this.$message.error('获取用户信息失败：无有效数据');
        }
      } catch (error) {
        this.$message.error('获取用户信息失败');
        console.error('获取用户信息失败:', error);
      }
      const userId = this.user.id;

      axios.post(`/commodity/click/library?userId=${userId}`, {
        headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
      });
    },
    borrowBook(book) {
      if (!book.available) return;
      this.$set(book, 'available', false);
      this.$message.success(`成功借阅《${book.title}》！`);
    }
  }
}
</script>

<style scoped>
.detail-container {
  min-height: 100vh;
  background: radial-gradient(ellipse at center, #001e3c 0%, #000 100%);
  color: #fff;
}
.header-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 30px;
  background: #002d52;
}
.logo {
  font-size: 20px;
  color: #4fc3f7;
}
.nav {
  display: flex;
  gap: 20px;
}
.nav a {
  color: #fff;
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s ease;
}
.nav a:hover {
  color: #4fc3f7;
}
.back-bar {
  margin: 20px 0 0 0;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}
.detail-main {
  max-width: 900px;
  margin: 40px auto;
  background: #13263a;
  border-radius: 10px;
  padding: 40px 30px;
  box-shadow: 0 5px 15px rgba(79, 195, 247, 0.1);
}
.search-section {
  max-width: 600px;
  margin: 0 auto 30px;
}
.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}
.book-card {
  background: #0a2744;
  border-radius: 10px;
  overflow: hidden;
  transition: transform 0.3s ease;
  border: 1px solid #1e88e5;
}
.book-card:hover {
  transform: translateY(-5px);
}
.book-cover {
  height: 180px;
  overflow: hidden;
}
.book-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.book-info {
  padding: 20px;
}
.book-info h3 {
  color: #4fc3f7;
  margin: 0 0 10px;
  font-size: 18px;
}
.book-info p {
  margin: 5px 0;
  color: #b0bec5;
}
.book-status {
  margin: 15px 0;
}
.status-available {
  color: #4caf50;
}
.status-borrowed {
  color: #f44336;
}
.borrow-btn {
  width: 100%;
  background: #1e88e5;
  border-color: #1e88e5;
}
.borrow-btn:hover {
  background: #1976d2;
  border-color: #1976d2;
}
.borrow-btn:disabled {
  background: #0a2744;
  border-color: #1e88e5;
  color: #b0bec5;
}
</style>
